package com.example.userregister;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ShowActivity extends AppCompatActivity {

    private TextView etAct,etPwd;
    private TextView myLife,myAttack,mySpeed;
    ProgressBar lifeProgress,attackProgress,speedProgress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        //创建 获取开启次页面的意图
        Intent intent = getIntent();
        //根据key取出对应的value值
        String account =  intent.getStringExtra("account");
        String password =  intent.getStringExtra("password");
        //获取次页面的控件
        etAct = findViewById(R.id.et_act2);
        etPwd = findViewById(R.id.et_pwd2);
        //把value显示到控件上
        etAct.setText(account);
        etPwd.setText(password);

        //初始化进度条
        initProgress();
    }

    private void initProgress() {
        //找到进度条控件
        lifeProgress = findViewById(R.id.life_progress);
        attackProgress = findViewById(R.id.attack_progress);
        speedProgress = findViewById(R.id.speed_progress);

        myLife = findViewById(R.id.life_value);
        myAttack = findViewById(R.id.attack_value);
        mySpeed = findViewById(R.id.speed_value);

        //设置进度条最大值
        lifeProgress.setMax(100);
        attackProgress.setMax(100);
        speedProgress.setMax(100);
    }

    public void click(View v){
        Intent intent = new Intent(this,ShopActivity.class);
        //开启新的Activity并拿到返回的结果值
        startActivityForResult(intent,1);
    }

    //当开启新页面关闭时执行这个方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(data!=null){
            if(requestCode == 1) {
                if (resultCode == 1) {
                    equipItem info = (equipItem) data.getSerializableExtra("equip");
                    //更新属性值
                    updateProgress(info);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void updateProgress(equipItem equip) {
        int lifeValue = lifeProgress.getProgress();
        int attackValue = attackProgress.getProgress();
        int speedValue = speedProgress.getProgress();

        //更改值
        lifeProgress.setProgress(lifeValue+equip.getLife());
        attackProgress.setProgress(attackValue+equip.getAttack());
        speedProgress.setProgress(speedValue+equip.getSpeed());

        //一定要加空格字符！！！
        myLife.setText(lifeProgress.getProgress()+" ");
        myAttack.setText(attackProgress.getProgress()+" ");
        mySpeed.setText(speedProgress.getProgress()+" ");
    }
}
